﻿# -*- coding:utf-8 -*-
import os
from django.db import models
from django.contrib.auth.models import User
from echo.utils import thumbnail
from django.conf import settings



def getTransliterateFilename(file_field):
    import echo.trans
    filetail = os.path.split(file_field.path)[1]        
    basename, format = os.path.splitext(filetail)
    return os.path.join(file_field.field.upload_to, ('%s%s' %  (basename.encode('trans/slug'),format)))




class Author(models.Model):
    
    surname = models.CharField(max_length=64,  verbose_name="Фамилия")
    first_name = models.CharField(max_length=64,  verbose_name="Имя")
    middle_name = models.CharField(max_length=64,  verbose_name="Отчество")
    bio =  models.TextField(verbose_name="Сведения об авторе") 
    person_photo = models.ImageField(upload_to='files/echo/img/', verbose_name="Фотография автора")  

    xml_file = models.FilePathField(path=settings.ECHO_AUTHOR_PATH, verbose_name="Файл XML")
    saved_ftp = models.BooleanField(verbose_name="Опубликовано на FTP")
    
    date = models.DateTimeField(auto_now_add=True, null=True, verbose_name="Дата") 
    user = models.ForeignKey(User, verbose_name="Пользователь")
    
    class Meta:
        ordering = ['surname']
        
        
    def __unicode__(self):
        return u"%s %s %s" % (self.surname, self.first_name, self.middle_name)
        
        
    def preview_person_photo(self):
        if not self.person_photo:
            return 'Нет изображения'
        image_path = thumbnail(self.person_photo, '300x300', 'p%s' % self.id)
        return u'%s' % image_path

    def preview_person_photo_small(self):
        if not self.person_photo:
            return 'Нет изображения'
        image_path = thumbnail(self.person_photo, '96x96', 'p%ss' % self.id)
        return u'%s' % image_path

    def admin_person_photo(self):
        return '<img src="%s"/>' % self.preview_person_photo_small()

    def fio(self):
        return u"%s %s %s" % (self.surname, self.first_name, self.middle_name)
    

    preview_person_photo.short_description = 'Картинка'
    preview_person_photo.allow_tags = True
    preview_person_photo_small.short_description = 'Картинка'
    preview_person_photo_small.allow_tags = True    
    admin_person_photo.allow_tags = True
    fio.short_description = 'ФИО'
        
    def save(self):
        self.person_photo.name = getTransliterateFilename(self.person_photo)
        super(Author, self).save()         
        
        
        

class Item(models.Model):
    author = models.ForeignKey(Author, verbose_name="Автор")
    item_title = models.CharField(max_length=128, blank=True, verbose_name="Название экспоната")
    item_fulltitle = models.CharField(max_length=512, verbose_name="Полное название экспоната")
    description = models.TextField(verbose_name="Описание предмета") 
    item_photo = models.ImageField(upload_to='files/echo/img/',  verbose_name="Фотография предмета")   
    publ_date = models.DateTimeField(null=True, blank=True, verbose_name="Дата публикации")     

    xml_file = models.FilePathField(path=settings.ECHO_ITEM_PATH, verbose_name="Файл XML")
    saved_ftp = models.BooleanField(verbose_name="Опубликовано на FTP")      
       
    date = models.DateTimeField(auto_now_add=True, null=True, verbose_name="Дата")
    user = models.ForeignKey(User, verbose_name="Пользователь")
    
    class Meta:
        pass
        
    def __unicode__(self):
        return u"%s" % self.item_title
        

    def preview_item_photo(self):
        if not self.item_photo:
            return 'Нет изображения'
        image_path = thumbnail(self.item_photo, '300x300', 'i%s' % self.id)
        return u'%s' % image_path

    def preview_item_photo_small(self):
        if not self.item_photo:
            return 'Нет изображения'
        image_path = thumbnail(self.item_photo, '96x96', 'i%ss' % self.id)
        return u'%s' % image_path

    preview_item_photo.short_description = 'Картинка'
    preview_item_photo.allow_tags = True
    preview_item_photo_small.short_description = 'Картинка'   
    preview_item_photo_small.allow_tags = True   

    def save(self):
        self.item_photo.name = getTransliterateFilename(self.item_photo)
        super(Item, self).save() 
        
        
class Variables(models.Model):        
    name = models.CharField(max_length=32, verbose_name="Имя")
    value = models.CharField(max_length=256, blank=True, verbose_name="Значение")
    description =  models.CharField(max_length=512, blank=True, verbose_name="Описание")
    
    class Meta:
        pass
        
    def __unicode__(self):
        return u"%s = %s (%s)" % (self.name, self.value, self.description)
    
    